﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
//using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_EmployeeInquire : System.Web.UI.Page
    {
        private IJobSiteBLL jobsiteBLL = new JobSiteBLL();
        private IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL ptBLL = new PositionTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IClassBLL classBLL = new ClassBLL();
        private IMasEducationDegreeBLL dBLL = new MasEducationDegreeBLL();
        private IMasEducationQualificationBLL qualBLL = new MasEducationQualificationBLL();
        private IMasEducationMajorBLL majorBLL = new MasEducationMajorBLL();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindEmpType();
                FillDataToJobSite();
                ConfigureListBoxPositionType();
            }
        }

        private void ConfigureListBoxPositionType()
        {
            int nEmpType = cmbEmployeeType.SelectedValue.Length > 0 ? Int32.Parse(cmbEmployeeType.SelectedValue) : 0;
            IList<PositionTypeInfo> infoList = ptBLL.ListPositionTypes(nEmpType);
            cmbPosType.DataSource = infoList;
            cmbPosType.DataBind();
        }
        private void ConfigureListBoxPosition()
        {
            DataSet ds = fieldBLL.LoadFields("", "", cmbEmployeeType.SelectedValue);
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                selectFieldPosition.DataSource = dt;
                selectFieldPosition.DataBind();
            }
        }
        private void ConfigureListBoxDegree()
        {
            //RadListBoxDestination.AllowReorder = AllowReorder.Checked;
            //RadListBoxDestination.AllowDelete = AllowDelete.Checked;
        }
        private void ConfigureListBoxQualification()
        {
            DataSet ds = qualBLL.LoadEducationQualifications((int?)null, (int?)null, "");
            DataTable dt = new DataTable();
            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
                selectQual.DataSource = dt;
                selectQual.DataBind();
            }
        }
        private void ConfigureListBoxMajor()
        {
            IList<MasEducationMajorInfo> infoList = majorBLL.ListEducationMajors();
            selectMajor.DataSource = infoList;
            selectMajor.DataBind();
        }
        private void ConfigureListBoxInstitutes()
        {
            //DataSet ds = insBLL.LoadEducationInstitutes((int?)null, );
            //RadListBoxDestination.AllowReorder = AllowReorder.Checked;
            //RadListBoxDestination.AllowDelete = AllowDelete.Checked;
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = etBLL.ListEmployeeTypes();
            cmbEmployeeType.DataSource = infoList;
            cmbEmployeeType.DataBind();
        }

        protected void FillDataToJobSite()
        {
            IList<JobSiteInfo> InfoList = jobsiteBLL.ListJobSiteByTypes(SystemInfo._JobsiteInternal);// .LoadListJobSite(null, Info.External);

            cmbJobSite.DataSource = InfoList;
            cmbJobSite.DataBind();
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            //DataTable dt = null;
            //string EmpTypeSeq = cmbEmployeeType.SelectedValue;
            //string JobSiteSeq = cmbJobSite.SelectedValue;
            ////DataSet ds = empBLL.LoadEmployees((int?)null, EmpTypeSeq, true);
            //DataSet ds = empBLL.LoadToExcel(EmpTypeSeq, JobSiteSeq, "", "", "", "", true);
            //if (ds.Tables.Count > 0)
            //    dt = ds.Tables[0].Copy();

            //ExportDataTableToExcel(dt, "Result");
        }
        private void ExportDataTableToExcel(DataTable dt, string FileName)
        {
            //Excel.Application oXL;
            //Excel.Workbook oWB;
            //Excel.Worksheet oSheet;
            //Excel.Range oRange;

            //oXL = new Excel.Application();

            //oXL.Visible = true;
            //oXL.DisplayAlerts = false;

            //oWB = oXL.Workbooks.Add(Missing.Value);

            //oSheet = (Excel.Worksheet)oWB.ActiveSheet;

            //int rowCount = 1;
            //foreach (DataRow dr in dt.Rows)
            //{
            //    rowCount += 1;
            //    for (int i = 1; i < dt.Columns.Count + 1; i++)
            //    {
            //        if (rowCount == 2)
            //        {
            //            oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
            //        }
            //        oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
            //    }
            //}

            //oRange = oSheet.get_Range(oSheet.Cells[1, 1],
            //              oSheet.Cells[rowCount, dt.Columns.Count]);
            //oRange.EntireColumn.AutoFit();

            //oSheet = null;
            //oRange = null;
            //oWB.SaveAs(FileName + ".xls", Excel.XlFileFormat.xlWorkbookNormal,
            //    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            //    Excel.XlSaveAsAccessMode.xlExclusive,
            //    Missing.Value, Missing.Value, Missing.Value,
            //    Missing.Value, Missing.Value);
            ////oWB.Close(Missing.Value, Missing.Value, Missing.Value);
            ////oWB = null;
            ////oXL.Quit();

            //GC.WaitForPendingFinalizers();
            //GC.Collect();
            //GC.WaitForPendingFinalizers();
            //GC.Collect();
        }

        protected void btnSelectPosType_Click(object sender, ImageClickEventArgs e)
        {
            foreach (RadComboBoxItem item in cmbPosType.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chk1");
                if (chk.Checked)
                    selectFieldPosition.Items.Add(item.Text);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (RadComboBoxItem item in cmbPosType.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chk1");
                if (chk.Checked)
                    selectFieldPosition.Items.Add(item.Text);
            }
        }
    }
}
